package cn.jly.spring.aop.jdkproxy;

import cn.jly.spring.aop.dao.CustomerDao;
import cn.jly.spring.aop.dao.impl.CustomerDaoImpl;

import java.lang.reflect.Proxy;


/**
 * @author lanyangji
 * @description jdk动态代理测试
 * @date 2020/11/10 下午 3:21
 */
public class JdkDynamicProxyTest {
    public static void main(String[] args) {
        // 被代理对象
        CustomerDao customerDao = new CustomerDaoImpl();
        // 创建代理对象
        final CustomerDao instance =
                (CustomerDao) Proxy.newProxyInstance(
                        JdkDynamicProxyTest.class.getClassLoader(),
                        new Class[]{CustomerDao.class},
                        new LoggerInvocationHandler(customerDao)
                );

        // 通过代理对象执行目标方法
        final int res = instance.add(1, 2);
        System.out.println("res = " + res);

        System.out.println("---------");
        final String res2 = instance.update("tom");
        System.out.println("res2 = " + res2);
    }
}
